package com.ruoyi.web.controller.construction;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.Strategy;
import com.ruoyi.system.service.construction.StrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/system/strategy")
public class StrategyController extends BaseController{

    @Autowired
    private StrategyService strategyService;
    @Autowired(required = false)
    private TokenService tokenService;
    /**
     * 查询施工攻略列表
     */
    @PreAuthorize("@ss.hasPermi('construction:strategy:list')")
    @GetMapping("/list")
    public TableDataInfo list(Strategy strategy)
    {
        startPage();
        List<Strategy> list = strategyService.selectStrategyList(strategy);
        return getDataTable(list);
    }

    /**
     * 导出施工攻略列表
     */
    @PreAuthorize("@ss.hasPermi('construction:strategy:export')")
    @Log(title = "施工攻略", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Strategy strategy)
    {
        List<Strategy> list = strategyService.selectStrategyList(strategy);
        ExcelUtil<Strategy> util = new ExcelUtil<Strategy>(Strategy.class);
        return util.exportExcel(list, "strategy");
    }

    /**
     * 获取施工攻略详细信息
     */
    @PreAuthorize("@ss.hasPermi('construction:strategy:query')")
    @GetMapping(value = "/{strategyId}")
    public AjaxResult getInfo(@PathVariable("strategyId") Long strategyId)
    {
        return AjaxResult.success(strategyService.selectStrategyById(strategyId));
    }

    /**
     * 新增施工攻略
     */
    @PreAuthorize("@ss.hasPermi('construction:strategy:add')")
    @Log(title = "施工攻略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Strategy strategy,@RequestParam("videoFile") MultipartFile file)throws IOException {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        strategy.setUserId(userId);

        String videoPath = RuoYiConfig.getVideoPath();
        String video = FileUploadUtils.uploadVideo(videoPath, file);
        strategy.setShipinUrl("http://39.98.237.139"+video);

        return toAjax(strategyService.insertStrategy(strategy));
    }

    /**
     * 修改施工攻略
     */
    @PreAuthorize("@ss.hasPermi('construction:strategy:edit')")
    @Log(title = "施工攻略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Strategy strategy)
    {
        return toAjax(strategyService.updateStrategy(strategy));
    }

    /**
     * 删除施工攻略
     */
    @PreAuthorize("@ss.hasPermi('construction:strategy:remove')")
    @Log(title = "施工攻略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{strategyIds}")
    public AjaxResult remove(@PathVariable Long[] strategyIds)
    {
        return toAjax(strategyService.deleteStrategyByIds(strategyIds));
    }
}
